﻿package igz.fleaxy.net {
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import igz.fleaxy.events.CommEvent;
	import igz.fleaxy.net.Comm;
	import igz.fleaxy.net.CommResponseType;
	import igz.fleaxy.util.ImageUtil;
	import igz.fleaxy.util.ObjectUtil;
	import igz.fleaxy.util.SpriteUtil;
	
	
	/**
	 * @author Intelygenz | www.intelygenz.com
	 *
	 * Clase ocupada de mostrar un "loader", es decir, un sprite que muestra el porcentaje de carga de un determinado elemento
	 */
	
	public class LoaderUi extends Sprite {

		protected var _Settings:Object;

		protected var _Loading:Sprite;
		protected var _Container:Sprite;
		protected var _Mask:DisplayObject;

		override public function get width():Number { return _Settings["width"]; }
		override public function get height():Number { return _Settings["height"]; }

		public function LoaderUi() {
			_Settings = { "width" : 16
						, "height" : 16
						, "loadingW" : 12
						, "loadingH" : 12
						, "onComplete" : null
						, "mask" : null
						, "autoCorrection" : false
						}

			_Container = new Sprite();
			addChild(_Container);

			_Loading = new Sprite();
			addChild(_Loading);

		}

		/**
		 * Pinta el loader y le añade el evento de entrada en frame
		 */
		protected function _DrawLoading() : void {
			SpriteUtil.$RemoveChildsOf(_Container);
			_Container.graphics.clear();
			_Container.graphics.beginFill(0xEEEEEE, 0);
			_Container.graphics.drawRect(0, 0, _Settings["width"], _Settings["height"]);
			_Container.graphics.endFill();

			_Loading.graphics.clear();
			_Loading.graphics.beginFill(0xEEEEEE, 0.8);
			_Loading.graphics.drawCircle(0, 0, (_Settings["loadingH"] / 2));

			_Loading.graphics.lineStyle(1, 0x000000, 1, true);
			_Loading.graphics.moveTo(0, 0);
			_Loading.graphics.lineTo(0, -(_Settings["loadingH"] / 2));

			_Loading.x = _Settings["width"] / 2;
			_Loading.y = _Settings["height"] / 2;

			addEventListener(Event.ENTER_FRAME, _Loading_OnEnterFrame);
		}

		protected function _Loading_OnEnterFrame(p_event:Event) : void {
			_Loading.rotation += 20;
		}

		/**
		 *  Carga la url que se le pase mostrando el loading para ver el progreso
		 * @param	p_uri url del elemento a cargar
		 * @param	p_settings : { "width" : 16
								, "height" : 16
								, "loadingW" : 12
								, "loadingH" : 12
								, "onComplete" : null
								, "mask" : null
								, "autoCorrection" : false
					}
		 */
		public function $Load(p_uri:String, p_settings:Object = null) : void {
			ObjectUtil.$Merge(p_settings, _Settings);
			_DrawLoading();
			Comm.$LoadFile(p_uri, { "onComplete" : _Load_OnComplete } );
		}

		protected function _Load_OnComplete(p_commEvent:CommEvent) : void {
			_Loading.graphics.clear();
			removeEventListener( Event.ENTER_FRAME, _Loading_OnEnterFrame );

			var dispObj:DisplayObject;
			switch ( p_commEvent.$CommResponseType ) {
				case CommResponseType.$BITMAP:
					dispObj = p_commEvent.$ResponseBitmap;
					(dispObj as Bitmap).smoothing = true;
					break;
				case CommResponseType.$SWF:
					dispObj = p_commEvent.$ResponseSwf;
					break;
			}
			
			if ( _Settings["autoCorrection"] ) {
				ImageUtil.$FitImageToBounds(dispObj, new Rectangle(0, 0, _Settings["width"], _Settings["height"]));
				dispObj.x = _Settings["width"] - dispObj.width >> 1;
				dispObj.y = _Settings["height"] - dispObj.height >> 1;
				if ( _Settings["mask"] == null ) {
					_Container.scrollRect = new Rectangle(0, 0, _Settings["width"], _Settings["height"]);
				}
			}
			else {
				dispObj.width  = _Settings["width"];
				dispObj.height = _Settings["height"];
			}

			_Container.addChild( dispObj );

			if ( _Settings["mask"] != null ) {
				_Mask = _Settings["mask"];
				_Mask.cacheAsBitmap = true;
				addChild(_Mask);
				_Container.cacheAsBitmap = true;
				_Container.mask = _Mask;
			}

			if ( _Settings["onComplete"]!=null ) {
				_Settings["onComplete"]();
			}

		}

	}

}